import time
#类装饰器
"""
装饰器本身既可以是函数也可以是类
装饰的对线既可以是函数也可以是类
"""
class Timer:
    def __init__(self, func):
        self.func = func
    def __call__(self, *args, **kwargs):
        start =time.time()
        ret=self.func(*args, **kwargs)
        print(f"Time:{time.time()-start}")
        return ret
#等价于
# add=Timer(add)
@Timer
def add(a,b):
    return a+b

print(add(3,5))

#带参数多类装饰器
class ParamDecorator:
    def __init__(self, prefix):  # 接收装饰器参数
        self.prefix = prefix

    def __call__(self, func):
        def wrapper(*args, **kwargs):
            print(f"{self.prefix}: Calling function {func.__name__}")
            result = func(*args, **kwargs)
            print(f"{self.prefix}: Function {func.__name__} returned {result}")
            return result
        return wrapper

@ParamDecorator("LOG")
def multiply(a, b):
    return a * b

print(multiply(2, 3))
# 输出:
# LOG: Calling function multiply
# LOG: Function multiply returned 6
# 6